package com.tjian.fafa.user.base.mainBody;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.ListView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.bean.TabMsgItemEntity;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.chatServices.ChatService;
import com.tjian.fafa.user.base.chatter.ChatActivity;

import java.util.List;

public class MainTabMyWebviewPage
{

    private static MainTabMyWebviewPage mInstance;

    private View mViewOfPage;
    private Context mContext0;

    private ListView mListView;
    private List<Integer> mListOfIds;
    private List<TabMsgItemEntity> mListOfEntity;

    private int mNotifId = 1;

    WebView wv;

    public static MainTabMyWebviewPage getInstance()
    {
        if (mInstance == null)
        {
            mInstance = new MainTabMyWebviewPage();
        }
        return mInstance;
    }

    private MainTabMyWebviewPage()
    {
    }

    public void onInit(View view, Context context)
    {
        mViewOfPage = view;
        mContext0 = context;
    }

    public List<TabMsgItemEntity> getListOfEntity()
    {
        return mListOfEntity;
    }

    public void onCreate()
    {

    }

    public void onUpdateByUserinfo(UserInfo user, String sentence, String time,
                                   boolean updateNotif, String imagePath)
    {
        if (mListOfIds.contains(user.getId()))
        {
            int idx = mListOfIds.indexOf(user.getId());
            mListOfIds.remove(idx);
            mListOfEntity.remove(idx);
        }
        mListOfIds.add(0, user.getId());
        mListOfEntity.add(0, new TabMsgItemEntity(user.getId(), user.getAvatarId(), user.getName(),
                sentence, time, imagePath));

        if (updateNotif)
        {
            onUpdateNotification(user.getId(), sentence);
        }
    }

    public void onUpdateById(int id, int avatarId, String name, String sentence, String time,
                             boolean updateNotif, String imagePath)
    {
        if (mListOfIds.contains(id))
        {
            int idx = mListOfIds.indexOf(id);
            mListOfIds.remove(idx);
            mListOfEntity.remove(idx);
        }
        mListOfIds.add(0, id);
        mListOfEntity.add(0, new TabMsgItemEntity(id, avatarId, name, sentence, time, imagePath));

        if (updateNotif)
        {
            onUpdateNotification(id, sentence);
        }
    }

    public void onUpdateView()
    {
        mListView.setAdapter(new MainTabMsgAdapter(mContext0, mListOfEntity));
    }

    @SuppressWarnings("deprecation")
    public void onUpdateNotification(int id, String sentence)
    {
        int icon = R.drawable.new_launcher;
        CharSequence tickerText = sentence;
        long when = System.currentTimeMillis();
        Notification mNotification = new Notification(icon, tickerText, when);

        mNotification.flags = Notification.FLAG_NO_CLEAR;
        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        if (!isForeGround())
        {
            mNotification.defaults |= Notification.DEFAULT_SOUND;
            mNotification.defaults |= Notification.DEFAULT_VIBRATE;
        }
        mNotification.contentView = null;

        Intent intent;
        if (id < 0)
        {
            intent = new Intent(mContext0,
                    FrdRequestNotifActivity.class);
        }
        else
        {
            intent = new Intent(mContext0,
                    ChatActivity.class);
            ChatService.getInstance().setEnterFromNotification(true);
            ChatService.getInstance().setEnterFromNotificationId(id);
        }

        PendingIntent contentIntent = PendingIntent.getActivity(
                mContext0, 0, intent, 0);

        mNotification = new Notification.Builder(mContext0)
                .setContentTitle("new message")
                .setContentText(sentence)
                .setContentIntent(contentIntent).build();

        NotificationManager mNotificationManager =
                (NotificationManager) mContext0.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(this.mNotifId, mNotification);
    }

    public boolean isForeGround()
    {
        ActivityManager am = (ActivityManager) mContext0.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
        String curPackageName = foregroundTaskInfo.baseActivity.getPackageName();

        String myPackageName = mContext0.getPackageName();

        if (curPackageName.equals(myPackageName))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
